package component.jfileexplorer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileFilter;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class JFileExplorer extends JPanel {

	private String rootLocation = System.getProperty("user.home");
	private JTree tree;
	
	FileFilter filter = new FileFilter() {
		@Override
		public boolean accept(File pathname) {
			return true;
		}
	};
	
	public static final FileFilter neuroFilter = new FileFilter() {
		@Override
		public boolean accept(File pathname) {
			
			// no unix hidden files/directories
			if (pathname.getName().startsWith(".")) return false;

			// directories
			if (pathname.isDirectory()) return true;
			
			// NeuroLucida files
			if (pathname.getName().endsWith(".ASC")) return true;
			// Netmorph files
			if (pathname.getName().endsWith(".neurons")) return true;
			
			return false;
		}
	};
	
	private class FileSystemModel extends DefaultTreeModel {
		
		public FileSystemModel(String root) {
			super(new FileNode(JFileExplorer.this, null, new File(root)));
		}		
	}
	

	public void setFilter(FileFilter filter) 
	{
		this.filter = filter;
		tree.setModel(new FileSystemModel(rootLocation));
	}
	

	public JFileExplorer() {

		setLayout(new BorderLayout());
				
		JComboBox locations = new JComboBox(
				new String[] { 
						"/", 
						System.getProperty("user.home"),
						"/Win7/Neuroscience/Data/",
						"C:\\", 
						"C:\\Neuroscience\\Data\\"
						});
		add(locations, BorderLayout.NORTH);
		locations.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					rootLocation = (String) e.getItem();
					tree.setModel(new FileSystemModel(rootLocation));
				}
			}
		});
		
		tree = new JTree(new FileSystemModel(rootLocation));
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				FileNode n = (FileNode) e.getPath().getLastPathComponent();
				if (selectionListener != null) selectionListener.actionPerformed(new ActionEvent(n.file, 0, "SELECTED"));
			}
		});
		tree.setFont(new Font("Arial", Font.PLAIN, 14));
		JScrollPane jsp = new JScrollPane(tree);
		add(jsp, BorderLayout.CENTER);
	}
	
	public FileNode getSelectedFile()
	{
		TreePath path = tree.getSelectionPath();
		if (path == null) return null;
		return (FileNode) path.getLastPathComponent();
	}

	ActionListener selectionListener;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame fr = new JFrame("FS Test");
		fr.setSize(500,500);
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JFileExplorer jfe = new JFileExplorer();
		jfe.setFilter(JFileExplorer.neuroFilter);
		fr.add(jfe);
		fr.setVisible(true);

	}


	public ActionListener getSelectionListener() {
		return selectionListener;
	}


	public void setSelectionListener(ActionListener selectionListener) {
		this.selectionListener = selectionListener;
	}

}
